<template>
  <div class="container">
    <div class="headerBox">
      <h5 class="f-c">修改我的登陆密码</h5>
    </div>
    <div class="formContainer">
      <el-form
        :label-position="labelPosition"
        label-width="100px"
        :model="changeForm"
        :rules="rules"
        ref="changeForm"
      >
        <el-form-item label="姓名">
          <el-input v-model="changeForm.tName" readonly></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="changeForm.tPhone" readonly></el-input>
        </el-form-item>
        <el-form-item label="原密码" prop="oldPaw">
          <el-input type="password"
            v-model="oldPaw"
            placeholder="请输入原来的密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd1">
          <el-input type="password" v-model="newPwd1" placeholder="请输入新的密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="newPwd2">
          <el-input type="password" v-model="newPwd2" placeholder="请再次输入新密码"></el-input>
        </el-form-item>
        <el-form-item style="border-top: 1px dashed #eee">
          <div style="margin-top: 20px">
            <el-button
              class="btn-save"
              @click="submitChange('changeForm')"
              >修改</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "ChangePass",
  data() {
    //检验旧密码
    let validateOldPwd = (rule, value, callback) => {
      if (this.oldPaw === "") {
        callback(new Error("请输入原密码"))
      } else if (this.oldPaw !== this.changeForm.tPasswrod) {
        callback(new Error("原密码输入错误"))
      } else {
        callback()
      }
    }
    //检验密码
    let validatePass = (rule, value, callback) => {
      if (this.newPwd1 === "") {
        callback(new Error("请输入新密码"))
      } else {
        if (this.newPwd2 !== "") {
          this.$refs.changeForm.validateField("checkPass")
        }
        callback()
      }
    }
    //检验再次输入密码
    let validatePass2 = (rule, value, callback) => {
      if (this.newPwd2 === "") {
        callback(new Error("请再次输入新密码"))
      } else if (this.newPwd2 !== this.newPwd1) {
        callback(new Error("两次输入密码不一致!"))
      } else {
        callback()
      }
    }
    return {
      oldPaw:'',
      loginUserInfo: {},
      changeForm: {},
      newPwd1: "",
      newPwd2: "",
      labelPosition: "right",
      rules: {
        oldPaw: [{ validator: validateOldPwd, trigger: "blur" }],
        newPwd1: [{ validator: validatePass, trigger: "blur" }],
        newPwd2: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    //拿到登陆人员最新信息
    async getNewInfo() {
      let nid = this.loginUserInfo.tId;
      await this.$axios
        .get(`/talents/showLoginAndPasswrod/${nid}`)
        .then((res) => {
          console.log(res)
          this.changeForm = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //修改密码请求
    async updatePassword() {
      let nid = this.loginUserInfo.tId;
      let passwrod = this.oldPaw;
      let newPassword = this.newPwd2;
      await this.$axios
        .put(`/talents/updateTalentsRegister/${nid}/${passwrod}/${newPassword}`)
        .then((res) => {
          if (res.data.msg === "修改成功") {
            this.$notify({
              title: "成功",
              message: "修改密码成功！！",
              type: "success",
              duration: 800,
              showClose: false,
            });
          }
        })
        .catch((err) => {
          this.$notify.error({
            title: "失败",
            message: "修改密码失败~~",
            duration: 800,
            showClose: false,
          })
        })
    },
    //提交表单验证
    submitChange(changeForm) {
      this.$refs[changeForm].validate((valid) => {
        if (valid) {
          this.updatePassword();
        } else {
          return false;
        }
      });
    },
  },
  created() {
    this.loginUserInfo = JSON.parse(localStorage.getItem("userInfo"));
  },
  mounted() {
    this.getNewInfo();
  },
};
</script>

<style scoped>
.container {
  width: 98%;
  height: 500px;
  margin: 0 auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.headerBox {
  width: 100%;
  height: 40px;
  padding: 10px;
  display: flex;
  align-items: center;
  font-size: 18px;
  border-bottom: 1px solid #cfd1d2;
  cursor: pointer;
}

.formContainer {
  width: 40%;
  margin: 15px 0 0 30px;
}

.f-c {
  color: #409eff;
}

.btn-save {
  background-color: #1ab394;
  border-color: #1ab394;
  color: #fff;
  margin: 0 20px;
}
</style>